package cookie

import (
	"gin-project-01/gin-step04/gin15-gorm-advance/controllers"
	"gin-project-01/gin-step04/gin15-gorm-advance/models"
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	"net/http"
)

type SessionController struct {
	controllers.BaseController //继承 controllers.BaseController
}

// Add
//
//	@Description: 设置session
//	@receiver u
//	@param c
func (u SessionController) Add(c *gin.Context) {
	sid := models.GenerateRandomString(32)
	session := sessions.Default(c)
	//设置过期时间
	session.Options(sessions.Options{
		MaxAge: 3600 * 6,
	})
	session.Set("sid", sid)
	session.Save()

	c.JSON(http.StatusOK, gin.H{
		"success": true,
	})
}

// Get
//
//	@Description: 获取session
//	@receiver u
//	@param c
func (u SessionController) Get(c *gin.Context) {
	session := sessions.Default(c)
	sid := session.Get("sid")
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"sid":     sid,
	})
}

// Del
//
//	@Description: 删除session
//	@receiver u
//	@param c
func (u SessionController) Del(c *gin.Context) {
	session := sessions.Default(c)
	session.Clear()
	session.Save()
	c.JSON(http.StatusOK, gin.H{
		"success": true,
	})
}
